//
//  GMPhotoView.m
//  VIPhotoViewDemo
//
//  Created by GL on 15/4/28.
//  Copyright (c) 2015年 vito. All rights reserved.
//

#import "GMPhotoView.h"
#import "VIPhotoView.h"
#import "GMTipsHelper.h"
#import "UIImageView+WebCache.h"

@interface GMPhotoView ()

@property (nonatomic, strong) UIWindow *window;
@property (nonatomic, strong) UIView *nView;
@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, assign) GMPhotoRightItemType itemType;

@end

@implementation GMPhotoView

-(UIWindow*)window
{
    if (!_window) {
        _window = [UIApplication sharedApplication].keyWindow;
    }
    return _window;
}

-(instancetype)initWithFrame:(CGRect)frame andImage:(UIImage*)image rightItemType:(GMPhotoRightItemType)rightItemType{
    
    if (self = [super initWithFrame:frame]) {
        
        //self.frame = self.window.bounds;
        self.backgroundColor = [UIColor blackColor];
        //[self.window addSubview:self];
        self.alpha = 0;
        
        _itemType = rightItemType;

        VIPhotoView *photoView = [[VIPhotoView alloc] initWithFrame:self.bounds andImage:image];
        photoView.autoresizingMask = (1 << 6) -1;
        photoView.backgroundColor = [UIColor clearColor];
        [self addSubview:photoView];
        
        _nView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, 64)];
        //_nView.backgroundColor = [UIColor blackColor];
        _nView.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.5];
        UIButton *leftBut = [UIButton buttonWithType:UIButtonTypeCustom];
        leftBut.frame = CGRectMake(0, 14, 60, 50);
        [leftBut setTintColor:[UIColor whiteColor]];
        leftBut.backgroundColor = [UIColor clearColor];
        [leftBut setImage:[UIImage imageNamed:@"back-White-Normal"] forState:UIControlStateNormal];
        [leftBut setImage:[UIImage imageNamed:@"back-White-TouchDown"] forState:UIControlStateHighlighted];
        [leftBut addTarget:self action:@selector(leftEvent:) forControlEvents:UIControlEventTouchUpInside];
        [_nView addSubview:leftBut];
        
        UIButton *rightBut = [UIButton buttonWithType:UIButtonTypeCustom];
        rightBut.frame = CGRectMake(_nView.frame.size.width - 60, 14, 60, 50);
        [rightBut addTarget:self action:@selector(rightEvent:) forControlEvents:UIControlEventTouchUpInside];
        rightBut.backgroundColor = [UIColor clearColor];
        rightBut.hidden = YES;
        [_nView addSubview:rightBut];
        [self addSubview:_nView];
        
        if (rightItemType == GMPhotoRightItemType_Delete) {
            rightBut.hidden = NO;
            [rightBut setImage:[UIImage imageNamed:@"PCDelegteImage"] forState:UIControlStateNormal];
        }
        
//        UITapGestureRecognizer *tapRecognizer1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCuttentView:)];
//        tapRecognizer1.numberOfTouchesRequired = 1; //手指数
//        tapRecognizer1.numberOfTapsRequired = 1; //tap次数
//        [self addGestureRecognizer:tapRecognizer1];
        
        UITapGestureRecognizer *tapRecognizer2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCuttentView2:)];
        tapRecognizer2.numberOfTouchesRequired = 1; //手指数
        tapRecognizer2.numberOfTapsRequired = 2; //tap次数
        [self addGestureRecognizer:tapRecognizer2];
        
        //如果不加下面的话，当单指双击时，会先调用单指单击中的处理，再调用单指双击中的处理
        //[tapRecognizer1 requireGestureRecognizerToFail:tapRecognizer2];
        
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame andImageUrl:(NSString*)imageUrl
{
    if (self = [super initWithFrame:frame]) {
        
        self.frame = self.window.bounds;
        self.backgroundColor = [UIColor blackColor];
        [self.window addSubview:self];
        //self.alpha = 0;
        
        self.imgView = [[UIImageView alloc] init];
        
        _nView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, 64)];
        _nView.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.5];
        UIButton *leftBut = [UIButton buttonWithType:UIButtonTypeCustom];
        leftBut.frame = CGRectMake(0, 14, 60, 50);
        [leftBut setTintColor:[UIColor whiteColor]];
        leftBut.backgroundColor = [UIColor clearColor];
        [leftBut setImage:[UIImage imageNamed:@"back-White-Normal"] forState:UIControlStateNormal];
        [leftBut setImage:[UIImage imageNamed:@"back-White-TouchDown"] forState:UIControlStateHighlighted];
        [leftBut addTarget:self action:@selector(leftEvent:) forControlEvents:UIControlEventTouchUpInside];
        [_nView addSubview:leftBut];
        
//        UIButton *rightBut = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//        rightBut.frame = CGRectMake(_nView.frame.size.width - 45, 25, 30, 30);
//        [rightBut addTarget:self action:@selector(rightEvent:) forControlEvents:UIControlEventTouchUpInside];
//        rightBut.hidden = YES;
//        [_nView addSubview:rightBut];
        
        [self addSubview:_nView];
        
//        UITapGestureRecognizer *tapRecognizer1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCuttentView:)];
//        tapRecognizer1.numberOfTouchesRequired = 1; //手指数
//        tapRecognizer1.numberOfTapsRequired = 1; //tap次数
//        [self addGestureRecognizer:tapRecognizer1];
        
        UITapGestureRecognizer *tapRecognizer2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCuttentView2:)];
        tapRecognizer2.numberOfTouchesRequired = 1; //手指数
        tapRecognizer2.numberOfTapsRequired = 2; //tap次数
        [self addGestureRecognizer:tapRecognizer2];
        
        //如果不加下面的话，当单指双击时，会先调用单指单击中的处理，再调用单指双击中的处理
        //[tapRecognizer1 requireGestureRecognizerToFail:tapRecognizer2];
        
        [GMTipsHelper showLoading];
        [self loadImageWithUrl:imageUrl];
        
    }
    return self;
}

-(void)loadImageWithUrl:(NSString *)imgUrl
{
    NSURL *iUrl = [NSURL URLWithString:imgUrl];
    //[self.imgView sd_setImageWithURL:imageUrl];
    [self.imgView sd_setImageWithURL:iUrl completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

        [GMTipsHelper showDismiss];
        if (!error) {
            [self showImage:image];
        }
    }];
}

-(void)showImage:(UIImage *)image
{
    VIPhotoView *photoView = [[VIPhotoView alloc] initWithFrame:self.bounds andImage:image];
    photoView.autoresizingMask = (1 << 6) -1;
    photoView.backgroundColor = [UIColor clearColor];
    [self addSubview:photoView];
    
    [self addSubview:_nView];
}

-(instancetype)initWithFrame:(CGRect)frame andImage:(UIImage*)image
{
    if (self = [super initWithFrame:frame]) {

        self.frame = self.window.bounds;
        self.backgroundColor = [UIColor blackColor];
        [self.window addSubview:self];
        self.alpha = 0;
        
        VIPhotoView *photoView = [[VIPhotoView alloc] initWithFrame:self.bounds andImage:image];
        photoView.autoresizingMask = (1 << 6) -1;
        photoView.backgroundColor = [UIColor clearColor];
        [self addSubview:photoView];

        _nView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, 64)];
        _nView.backgroundColor = [UIColor blackColor];
        UIButton *leftBut = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        leftBut.frame = CGRectMake(15, 25, 30, 30);
        [leftBut setTintColor:[UIColor whiteColor]];
        leftBut.backgroundColor = [UIColor clearColor];
        [leftBut setImage:[UIImage imageNamed:@"BackZoomImage"] forState:UIControlStateNormal];
        [leftBut addTarget:self action:@selector(leftEvent:) forControlEvents:UIControlEventTouchUpInside];
        [_nView addSubview:leftBut];
        
        UIButton *rightBut = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        rightBut.frame = CGRectMake(_nView.frame.size.width - 45, 25, 30, 30);
        [rightBut addTarget:self action:@selector(rightEvent:) forControlEvents:UIControlEventTouchUpInside];
        rightBut.hidden = YES;
        [_nView addSubview:rightBut];
        [self addSubview:_nView];
        
        UITapGestureRecognizer *tapRecognizer1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCuttentView:)];
        tapRecognizer1.numberOfTouchesRequired = 1; //手指数
        tapRecognizer1.numberOfTapsRequired = 1; //tap次数
        [self addGestureRecognizer:tapRecognizer1];
        
        UITapGestureRecognizer *tapRecognizer2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCuttentView2:)];
        tapRecognizer2.numberOfTouchesRequired = 1; //手指数
        tapRecognizer2.numberOfTapsRequired = 2; //tap次数
        [self addGestureRecognizer:tapRecognizer2];
        
        //如果不加下面的话，当单指双击时，会先调用单指单击中的处理，再调用单指双击中的处理
        [tapRecognizer1 requireGestureRecognizerToFail:tapRecognizer2];
        
    }
    return self;
}

-(void)tapCuttentView2:(UITapGestureRecognizer*)tap{
}

-(void)tapCuttentView:(UITapGestureRecognizer*)tap
{
    //DLog(@"tap.numberOfTouches :%lu",(unsigned long)tap.numberOfTouches);
    if (tap.numberOfTouches == 1) {
        if (_nView.alpha) {
            [self hidenNavicationView];
        }else{
            [self showNavicationView];
        }
    }
}

-(void)leftEvent:(UIButton*)_but
{
    self.nView.hidden = 0;
    [self hidenPhotoView];
}

-(void)rightEvent:(UIButton*)_but
{
    // 右边按钮
    if (_itemType == GMPhotoRightItemType_Delete) {
        if ([self.pDelegate respondsToSelector:@selector(tipDeletedImage)]) {
            [self.pDelegate tipDeletedImage];
        }
    }
}

-(void)deleteTip
{
}

-(void)showNavicationView
{
    [UIView animateWithDuration:0.5 animations:^{
        self.nView.alpha = 1;
    } completion:^(BOOL finished) {
    }];
}

-(void)hidenNavicationView
{
    [UIView animateWithDuration:0.5 animations:^{
        self.nView.alpha = 0;
    } completion:^(BOOL finished) {
    }];
}

-(void)showPhotoView
{
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
    } completion:^(BOOL finished) {
    }];
}

-(void)hidenPhotoView
{
    [GMTipsHelper showDismiss];
    self.nView.hidden = 0;

    [UIView animateWithDuration:0.1 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
